/**
 * 
 */
package bi.uc.client;

import java.util.HashMap;

import com.bi_uc.www.R;

import bi.uc.login.Info;
import bi.uc.pay.PayActivity;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Administrator
 * 
 */
public class MainActivity extends Activity implements OnClickListener {
	Info info = Info.getInfo();
	Intent intent;
	LinearLayout options[] = new LinearLayout[6];
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.e("tag", "onCreate");
		((ExitApplication)getApplication()).addActivity(this);
		setContentView(R.layout.mainactivity);
		int len = options.length;
		int size = info.optionsList.size();
		for (int i = 0; i < len; i++) {
			options[i] = (LinearLayout) findViewById(R.id.option1+i);
			if (i<size) {
				ImageView image = (ImageView)options[i].getChildAt(0);
				TextView text = (TextView) options[i].getChildAt(1);
				HashMap<String, Object> map = info.optionsList.get(i);				
				text.setText(map.get("text").toString());
				image.setBackgroundResource((Integer) map.get("image"));
				options[i].setOnClickListener(this);
			}
		}
	}
	
	@Override
	public void onClick(View view) {
		int i = view.getId()-R.id.option1;
		int key = (Integer) info.optionsList.get(i).get("image");
		switch (key) {
		case R.drawable.quickpay:
			intent = new Intent(this, QuickpayActivity.class);
			startActivityForResult(intent, i);
			break;
		case R.drawable.quickscroe:
			intent = new Intent(this, QuickscoreActivity.class);
			startActivityForResult(intent, i);
			break;
		case R.drawable.addmoney:
			intent = new Intent(this, AddMoneyActivity.class);
			startActivityForResult(intent, i);
			break;
		case R.drawable.pay:
			intent = new Intent(this, PayActivity.class);
			startActivityForResult(intent, i);
			break;
		case R.drawable.newmember:
			intent = new Intent(this, NewMemberActivity.class);
			startActivityForResult(intent, i);   
			break;
		default:
			break;
		}
	} 
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);		
		if(resultCode==Activity.RESULT_OK){
			Toast toast = Toast.makeText(this, info.optionsList.get(requestCode).get("text")+"成功！", Toast.LENGTH_SHORT);
			LinearLayout toastView = (LinearLayout) toast.getView();	        	   
			ImageView view = new ImageView(this);
			view.setImageResource(R.drawable.success); 
			toastView.addView(view, 0);
			toast.setGravity(Gravity.CENTER, 0, 0);		        	  
			toast.show();
		}
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onBackPressed()
	 */
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		activityManager.killBackgroundProcesses(getPackageName());
		Log.e("tag", "stopservice");
//		((ExitApplication)getApplication()).exit();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
//		outState.putString("message", text.getText().toString());
		Log.e("tag", "onSaveInstanceState");
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
		int size = info.optionsList.size();
		for (int i = 0; i < size; i++) {
				ImageView image = (ImageView)options[i].getChildAt(0);
				TextView text = (TextView) options[i].getChildAt(1);
				HashMap<String, Object> map = info.optionsList.get(i);				
				text.setText(map.get("text").toString());
				image.setBackgroundResource((Integer) map.get("image"));
		}
		Log.e("tag", "onRestoreInstanceState");
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.e("tag", "onDestroy");
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.e("tag", "onStop");
	}
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.e("tag", "onPause");
	}
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.e("tag", "onRestart");
	}
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.e("tag", "onResume");
	}
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.e("tag", "onStart");
	}
	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
/*	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		  case 0:
		          if(resultCode==Activity.RESULT_OK){
		        	  Toast toast = Toast.makeText(this, "新建会员成功！", Toast.LENGTH_SHORT);
		        	  LinearLayout toastView = (LinearLayout) toast.getView();	        	   
		        	  ImageView view = new ImageView(this);
		        	  view.setImageResource(R.drawable.newmember_success); 
		        	  toastView.addView(view, 0);
		        	  toast.setGravity(Gravity.CENTER, 0, 0);		        	  
		        	  toast.show();
		          }
		          break;
		  case 1:
	          if(resultCode==Activity.RESULT_OK){
	        	  Toast toast = Toast.makeText(this, "充值成功！", Toast.LENGTH_SHORT);
	        	  LinearLayout toastView = (LinearLayout) toast.getView();	        	   
	        	  ImageView view = new ImageView(this);
	        	  view.setImageResource(R.drawable.newmember_success); 
	        	  toastView.addView(view, 0);
	        	  toast.setGravity(Gravity.CENTER, 0, 0);		        	  
	        	  toast.show();
	          }
	          break;
		  case 2:
	          if(resultCode==Activity.RESULT_OK){
	        	  Toast toast = Toast.makeText(this, "完成快速积分！", Toast.LENGTH_SHORT);
	        	  LinearLayout toastView = (LinearLayout) toast.getView();	        	   
	        	  ImageView view = new ImageView(this);
	        	  view.setImageResource(R.drawable.newmember_success); 
	        	  toastView.addView(view, 0);
	        	  toast.setGravity(Gravity.CENTER, 0, 0);		        	  
	        	  toast.show();
	          }
	          break;
		  case 3:
	          if(resultCode==Activity.RESULT_OK){
	        	  Toast toast = Toast.makeText(this, "快速结账成功!", Toast.LENGTH_SHORT);
	        	  LinearLayout toastView = (LinearLayout) toast.getView();	        	   
	        	  ImageView view = new ImageView(this);
	        	  view.setImageResource(R.drawable.newmember_success); 
	        	  toastView.addView(view, 0);
	        	  toast.setGravity(Gravity.CENTER, 0, 0);		        	  
	        	  toast.show();
	          }
	          break;
		  }
	}*/
	
}
